package cn.lagou.homework1

import org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object GraphX {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().
      setAppName(this.getClass.getCanonicalName)
      .setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("warn")

    val vertexArray: Array[(Long, String)] = Array((1L, "SFO"), (2L, "ORD"), (3L, "DFW"))
    val edgeArray: Array[Edge[Int]] = Array(
      Edge(1L, 2L, 1800),
      Edge(2L, 3L, 800),
      Edge(3L, 1L, 1400)
    )
    val vertexRDD: RDD[(VertexId, String)] = sc.makeRDD(vertexArray)
    val edgeRDD: RDD[Edge[Int]] = sc.makeRDD(edgeArray)

    // 生成图
    val graph = Graph(vertexRDD, edgeRDD)

    // 所有顶点
    println("所有顶点:")
    graph.vertices.foreach(println)

    // 所有边
    println("所有边：")
    graph.edges.foreach(println)

    // 所有的triplets
    println("所有的三元组信息：")
    graph.triplets.foreach(println)

    // 求顶点数
    val vertexCount = graph.vertices.count()
    println(s"总顶点数：$vertexCount")

    // 求边数
    val edgeCount = graph.edges.count()
    println(s"总边数：$edgeCount")

    //机场距离大于1000的
    println("机场距离大于1000的边的信息：")
    graph.edges.filter(_.attr > 1000).foreach(println)

    // 按所有机场之间的距离排序（降序）
    println("降序排列机场之间的距离：")
    graph.edges.sortBy(-_.attr).foreach(println)
  }
}
